﻿using AutoMapper;
using Sustainalytics.Entities.RiskRatings;
using Sustainalytics.Utils;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace Sustainalytics.ClientDW.Synchronization.SyncRiskRatingsData.Sources
{
    public class RiskRatingIndicatorSource
    {
        private static readonly IndicatorCodeComparer indicatorComparer = new IndicatorCodeComparer();
        public static IEnumerable<RiskRatingSource> Map(
            IEnumerable<ComprehensiveRiskRatingIndicator> riskRatingIndicators)
        {
            Dictionary<Guid, Dictionary<string, List<string>>> companySourceDictionary = new Dictionary<Guid, Dictionary<string, List<string>>>();

            foreach (var indicator in riskRatingIndicators)
            {
                if (!companySourceDictionary.TryGetValue(indicator.CompanyId, out Dictionary<string, List<string>> sourceDictionary))
                {
                    sourceDictionary = new Dictionary<string, List<string>>();
                    companySourceDictionary.Add(indicator.CompanyId, sourceDictionary);
                }

                if (indicator.Sources != null && indicator.Sources.Any())
                {
                    foreach (var source in indicator.Sources)
                    {
                        if (!sourceDictionary.TryGetValue(source, out List<string> indicatorCodes))
                        {
                            indicatorCodes = new List<string>();
                            sourceDictionary.Add(source, indicatorCodes);
                        }

                        indicatorCodes.Add(indicator.Code);
                    };
                }
            };

            return companySourceDictionary.Select((KeyValuePair<Guid, Dictionary<string, List<string>>> x) => new RiskRatingSource()
            {
                Id = x.Key,
                Sources = x.Value.Select((KeyValuePair<string, List<string>> y) => new Source()
                {
                    Indicators = y.Value.Distinct().OrderBy(ind => ind, indicatorComparer).ToArray(),
                    Text = y.Key

                }).OrderBy(src => src.Text).ToList()
            });
        }

        public static IEnumerable<RiskRatingSource> Map(
           IEnumerable<CoreRiskRatingIndicator> riskRatingIndicators)
        {
            Dictionary<Guid, Dictionary<string, List<string>>> companySourceDictionary = new Dictionary<Guid, Dictionary<string, List<string>>>();

            foreach (var indicator in riskRatingIndicators)
            {
                if (!companySourceDictionary.TryGetValue(indicator.CompanyId, out Dictionary<string, List<string>> sourceDictionary))
                {
                    sourceDictionary = new Dictionary<string, List<string>>();
                    companySourceDictionary.Add(indicator.CompanyId, sourceDictionary);
                }

                if (indicator.Sources != null && indicator.Sources.Any())
                {
                    foreach (var source in indicator.Sources)
                    {
                        if (!sourceDictionary.TryGetValue(source, out List<string> indicatorCodes))
                        {
                            indicatorCodes = new List<string>();
                            sourceDictionary.Add(source, indicatorCodes);
                        }

                        indicatorCodes.Add(indicator.Code);
                    };
                }
            };

            return companySourceDictionary.Select((KeyValuePair<Guid, Dictionary<string, List<string>>> x) => new RiskRatingSource()
            {
                Id = x.Key,
                Sources = x.Value.Select((KeyValuePair<string, List<string>> y) => new Source()
                {
                    Indicators = y.Value.Distinct().OrderBy(ind => ind, indicatorComparer).ToArray(),
                    Text = y.Key

                }).OrderBy(src => src.Text).ToList()
            });
        }
    }
}
